<!DOCTYPE html><html><head><meta charset="utf-8"><style>body {
  width: 45em;
  border: 1px solid #ddd;
  outline: 1300px solid #fff;
  margin: 16px auto;
}

body .markdown-body
{
  padding: 30px;
}

@font-face {
  font-family: fontawesome-mini;
  src: url(data:font/woff;charset=utf-8;base64,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) format('woff');
}

@font-face {
  font-family: octicons-anchor;
  src: url(data:font/woff;charset=utf-8;base64,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) format('woff');
}

.markdown-body {
  font-family: sans-serif;
  -ms-text-size-adjust: 100%;
  -webkit-text-size-adjust: 100%;
  color: #333333;
  overflow: hidden;
  font-family: "Helvetica Neue", Helvetica, "Segoe UI", Arial, freesans, sans-serif;
  font-size: 16px;
  line-height: 1.6;
  word-wrap: break-word;
}

.markdown-body a {
  background: transparent;
}

.markdown-body a:active,
.markdown-body a:hover {
  outline: 0;
}

.markdown-body b,
.markdown-body strong {
  font-weight: bold;
}

.markdown-body mark {
  background: #ff0;
  color: #000;
  font-style: italic;
  font-weight: bold;
}

.markdown-body sub,
.markdown-body sup {
  font-size: 75%;
  line-height: 0;
  position: relative;
  vertical-align: baseline;
}
.markdown-body sup {
  top: -0.5em;
}
.markdown-body sub {
  bottom: -0.25em;
}

.markdown-body h1 {
  font-size: 2em;
  margin: 0.67em 0;
}

.markdown-body img {
  border: 0;
}

.markdown-body hr {
  -moz-box-sizing: content-box;
  box-sizing: content-box;
  height: 0;
}

.markdown-body pre {
  overflow: auto;
}

.markdown-body code,
.markdown-body kbd,
.markdown-body pre,
.markdown-body samp {
  font-family: monospace, monospace;
  font-size: 1em;
}

.markdown-body input {
  color: inherit;
  font: inherit;
  margin: 0;
}

.markdown-body html input[disabled] {
  cursor: default;
}

.markdown-body input {
  line-height: normal;
}

.markdown-body input[type="checkbox"] {
  box-sizing: border-box;
  padding: 0;
}

.markdown-body table {
  border-collapse: collapse;
  border-spacing: 0;
}

.markdown-body td,
.markdown-body th {
  padding: 0;
}

.markdown-body .codehilitetable {
  border: 0;
  border-spacing: 0;
}

.markdown-body .codehilitetable tr {
  border: 0;
}

.markdown-body .codehilitetable pre,
.markdown-body .codehilitetable div.codehilite {
  margin: 0;
}

.markdown-body .linenos,
.markdown-body .code,
.markdown-body .codehilitetable td {
  border: 0;
  padding: 0;
}

.markdown-body td:not(.linenos) .linenodiv {
  padding: 0 !important;
}

.markdown-body .code {
  width: 100%;
}

.markdown-body .linenos div pre,
.markdown-body .linenodiv pre,
.markdown-body .linenodiv {
  border: 0;
  -webkit-border-radius: 0;
  -moz-border-radius: 0;
  border-radius: 0;
  -webkit-border-top-left-radius: 3px;
  -webkit-border-bottom-left-radius: 3px;
  -moz-border-radius-topleft: 3px;
  -moz-border-radius-bottomleft: 3px;
  border-top-left-radius: 3px;
  border-bottom-left-radius: 3px;
}

.markdown-body .code div pre,
.markdown-body .code div {
  border: 0;
  -webkit-border-radius: 0;
  -moz-border-radius: 0;
  border-radius: 0;
  -webkit-border-top-right-radius: 3px;
  -webkit-border-bottom-right-radius: 3px;
  -moz-border-radius-topright: 3px;
  -moz-border-radius-bottomright: 3px;
  border-top-right-radius: 3px;
  border-bottom-right-radius: 3px;
}

.markdown-body * {
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

.markdown-body input {
  font: 13px Helvetica, arial, freesans, clean, sans-serif, "Segoe UI Emoji", "Segoe UI Symbol";
  line-height: 1.4;
}

.markdown-body a {
  color: #4183c4;
  text-decoration: none;
}

.markdown-body a:hover,
.markdown-body a:focus,
.markdown-body a:active {
  text-decoration: underline;
}

.markdown-body hr {
  height: 0;
  margin: 15px 0;
  overflow: hidden;
  background: transparent;
  border: 0;
  border-bottom: 1px solid #ddd;
}

.markdown-body hr:before,
.markdown-body hr:after {
  display: table;
  content: " ";
}

.markdown-body hr:after {
  clear: both;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
  margin-top: 15px;
  margin-bottom: 15px;
  line-height: 1.1;
}

.markdown-body h1 {
  font-size: 30px;
}

.markdown-body h2 {
  font-size: 21px;
}

.markdown-body h3 {
  font-size: 16px;
}

.markdown-body h4 {
  font-size: 14px;
}

.markdown-body h5 {
  font-size: 12px;
}

.markdown-body h6 {
  font-size: 11px;
}

.markdown-body blockquote {
  margin: 0;
}

.markdown-body ul,
.markdown-body ol {
  padding: 0;
  margin-top: 0;
  margin-bottom: 0;
}

.markdown-body ol ol,
.markdown-body ul ol {
  list-style-type: lower-roman;
}

.markdown-body ul ul ol,
.markdown-body ul ol ol,
.markdown-body ol ul ol,
.markdown-body ol ol ol {
  list-style-type: lower-alpha;
}

.markdown-body dd {
  margin-left: 0;
}

.markdown-body code,
.markdown-body pre,
.markdown-body samp {
  font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
  font-size: 12px;
}

.markdown-body pre {
  margin-top: 0;
  margin-bottom: 0;
}

.markdown-body kbd {
  background-color: #e7e7e7;
  background-image: -moz-linear-gradient(#fefefe, #e7e7e7);
  background-image: -webkit-linear-gradient(#fefefe, #e7e7e7);
  background-image: linear-gradient(#fefefe, #e7e7e7);
  background-repeat: repeat-x;
  border-radius: 2px;
  border: 1px solid #cfcfcf;
  color: #000;
  padding: 3px 5px;
  line-height: 10px;
  font: 11px Consolas, "Liberation Mono", Menlo, Courier, monospace;
  display: inline-block;
}

.markdown-body>*:first-child {
  margin-top: 0 !important;
}

.markdown-body>*:last-child {
  margin-bottom: 0 !important;
}

.markdown-body .headeranchor-link {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  display: block;
  padding-right: 6px;
  padding-left: 30px;
  margin-left: -30px;
}

.markdown-body .headeranchor-link:focus {
  outline: none;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
  position: relative;
  margin-top: 1em;
  margin-bottom: 16px;
  font-weight: bold;
  line-height: 1.4;
}

.markdown-body h1 .headeranchor,
.markdown-body h2 .headeranchor,
.markdown-body h3 .headeranchor,
.markdown-body h4 .headeranchor,
.markdown-body h5 .headeranchor,
.markdown-body h6 .headeranchor {
  display: none;
  color: #000;
  vertical-align: middle;
}

.markdown-body h1:hover .headeranchor-link,
.markdown-body h2:hover .headeranchor-link,
.markdown-body h3:hover .headeranchor-link,
.markdown-body h4:hover .headeranchor-link,
.markdown-body h5:hover .headeranchor-link,
.markdown-body h6:hover .headeranchor-link {
  height: 1em;
  padding-left: 8px;
  margin-left: -30px;
  line-height: 1;
  text-decoration: none;
}

.markdown-body h1:hover .headeranchor-link .headeranchor,
.markdown-body h2:hover .headeranchor-link .headeranchor,
.markdown-body h3:hover .headeranchor-link .headeranchor,
.markdown-body h4:hover .headeranchor-link .headeranchor,
.markdown-body h5:hover .headeranchor-link .headeranchor,
.markdown-body h6:hover .headeranchor-link .headeranchor {
  display: inline-block;
}

.markdown-body h1 {
  padding-bottom: 0.3em;
  font-size: 2.25em;
  line-height: 1.2;
  border-bottom: 1px solid #eee;
}

.markdown-body h2 {
  padding-bottom: 0.3em;
  font-size: 1.75em;
  line-height: 1.225;
  border-bottom: 1px solid #eee;
}

.markdown-body h3 {
  font-size: 1.5em;
  line-height: 1.43;
}

.markdown-body h4 {
  font-size: 1.25em;
}

.markdown-body h5 {
  font-size: 1em;
}

.markdown-body h6 {
  font-size: 1em;
  color: #777;
}

.markdown-body p,
.markdown-body blockquote,
.markdown-body ul,
.markdown-body ol,
.markdown-body dl,
.markdown-body table,
.markdown-body pre,
.markdown-body .admonition {
  margin-top: 0;
  margin-bottom: 16px;
}

.markdown-body hr {
  height: 4px;
  padding: 0;
  margin: 16px 0;
  background-color: #e7e7e7;
  border: 0 none;
}

.markdown-body ul,
.markdown-body ol {
  padding-left: 2em;
}

.markdown-body ul ul,
.markdown-body ul ol,
.markdown-body ol ol,
.markdown-body ol ul {
  margin-top: 0;
  margin-bottom: 0;
}

.markdown-body li>p {
  margin-top: 16px;
}

.markdown-body dl {
  padding: 0;
}

.markdown-body dl dt {
  padding: 0;
  margin-top: 16px;
  font-size: 1em;
  font-style: italic;
  font-weight: bold;
}

.markdown-body dl dd {
  padding: 0 16px;
  margin-bottom: 16px;
}

.markdown-body blockquote {
  padding: 0 15px;
  color: #777;
  border-left: 4px solid #ddd;
}

.markdown-body blockquote>:first-child {
  margin-top: 0;
}

.markdown-body blockquote>:last-child {
  margin-bottom: 0;
}

.markdown-body table {
  display: block;
  width: 100%;
  overflow: auto;
  word-break: normal;
  word-break: keep-all;
}

.markdown-body table th {
  font-weight: bold;
}

.markdown-body table th,
.markdown-body table td {
  padding: 6px 13px;
  border: 1px solid #ddd;
}

.markdown-body table tr {
  background-color: #fff;
  border-top: 1px solid #ccc;
}

.markdown-body table tr:nth-child(2n) {
  background-color: #f8f8f8;
}

.markdown-body img {
  max-width: 100%;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

.markdown-body code,
.markdown-body samp {
  padding: 0;
  padding-top: 0.2em;
  padding-bottom: 0.2em;
  margin: 0;
  font-size: 85%;
  background-color: rgba(0,0,0,0.04);
  border-radius: 3px;
}

.markdown-body code:before,
.markdown-body code:after {
  letter-spacing: -0.2em;
  content: "\00a0";
}

.markdown-body pre>code {
  padding: 0;
  margin: 0;
  font-size: 100%;
  word-break: normal;
  white-space: pre;
  background: transparent;
  border: 0;
}

.markdown-body .codehilite {
  margin-bottom: 16px;
}

.markdown-body .codehilite pre,
.markdown-body pre {
  padding: 16px;
  overflow: auto;
  font-size: 85%;
  line-height: 1.45;
  background-color: #f7f7f7;
  border-radius: 3px;
}

.markdown-body .codehilite pre {
  margin-bottom: 0;
  word-break: normal;
}

.markdown-body pre {
  word-wrap: normal;
}

.markdown-body pre code {
  display: inline;
  max-width: initial;
  padding: 0;
  margin: 0;
  overflow: initial;
  line-height: inherit;
  word-wrap: normal;
  background-color: transparent;
  border: 0;
}

.markdown-body pre code:before,
.markdown-body pre code:after {
  content: normal;
}

/* Admonition */
.markdown-body .admonition {
  -webkit-border-radius: 3px;
  -moz-border-radius: 3px;
  position: relative;
  border-radius: 3px;
  border: 1px solid #e0e0e0;
  border-left: 6px solid #333;
  padding: 10px 10px 10px 30px;
}

.markdown-body .admonition table {
  color: #333;
}

.markdown-body .admonition p {
  padding: 0;
}

.markdown-body .admonition-title {
  font-weight: bold;
  margin: 0;
}

.markdown-body .admonition>.admonition-title {
  color: #333;
}

.markdown-body .attention>.admonition-title {
  color: #a6d796;
}

.markdown-body .caution>.admonition-title {
  color: #d7a796;
}

.markdown-body .hint>.admonition-title {
  color: #96c6d7;
}

.markdown-body .danger>.admonition-title {
  color: #c25f77;
}

.markdown-body .question>.admonition-title {
  color: #96a6d7;
}

.markdown-body .note>.admonition-title {
  color: #d7c896;
}

.markdown-body .admonition:before,
.markdown-body .attention:before,
.markdown-body .caution:before,
.markdown-body .hint:before,
.markdown-body .danger:before,
.markdown-body .question:before,
.markdown-body .note:before {
  font: normal normal 16px fontawesome-mini;
  -moz-osx-font-smoothing: grayscale;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  line-height: 1.5;
  color: #333;
  position: absolute;
  left: 0;
  top: 0;
  padding-top: 10px;
  padding-left: 10px;
}

.markdown-body .admonition:before {
  content: "\f056\00a0";
  color: 333;
}

.markdown-body .attention:before {
  content: "\f058\00a0";
  color: #a6d796;
}

.markdown-body .caution:before {
  content: "\f06a\00a0";
  color: #d7a796;
}

.markdown-body .hint:before {
  content: "\f05a\00a0";
  color: #96c6d7;
}

.markdown-body .danger:before {
  content: "\f057\00a0";
  color: #c25f77;
}

.markdown-body .question:before {
  content: "\f059\00a0";
  color: #96a6d7;
}

.markdown-body .note:before {
  content: "\f040\00a0";
  color: #d7c896;
}

.markdown-body .admonition::after {
  content: normal;
}

.markdown-body .attention {
  border-left: 6px solid #a6d796;
}

.markdown-body .caution {
  border-left: 6px solid #d7a796;
}

.markdown-body .hint {
  border-left: 6px solid #96c6d7;
}

.markdown-body .danger {
  border-left: 6px solid #c25f77;
}

.markdown-body .question {
  border-left: 6px solid #96a6d7;
}

.markdown-body .note {
  border-left: 6px solid #d7c896;
}

.markdown-body .admonition>*:first-child {
  margin-top: 0 !important;
}

.markdown-body .admonition>*:last-child {
  margin-bottom: 0 !important;
}

/* progress bar*/
.markdown-body .progress {
  display: block;
  width: 300px;
  margin: 10px 0;
  height: 24px;
  -webkit-border-radius: 3px;
  -moz-border-radius: 3px;
  border-radius: 3px;
  background-color: #ededed;
  position: relative;
  box-shadow: inset -1px 1px 3px rgba(0, 0, 0, .1);
}

.markdown-body .progress-label {
  position: absolute;
  text-align: center;
  font-weight: bold;
  width: 100%; margin: 0;
  line-height: 24px;
  color: #333;
  text-shadow: 1px 1px 0 #fefefe, -1px -1px 0 #fefefe, -1px 1px 0 #fefefe, 1px -1px 0 #fefefe, 0 1px 0 #fefefe, 0 -1px 0 #fefefe, 1px 0 0 #fefefe, -1px 0 0 #fefefe, 1px 1px 2px #000;
  -webkit-font-smoothing: antialiased !important;
  white-space: nowrap;
  overflow: hidden;
}

.markdown-body .progress-bar {
  height: 24px;
  float: left;
  -webkit-border-radius: 3px;
  -moz-border-radius: 3px;
  border-radius: 3px;
  background-color: #96c6d7;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .5), inset 0 -1px 0 rgba(0, 0, 0, .1);
  background-size: 30px 30px;
  background-image: -webkit-linear-gradient(
    135deg, rgba(255, 255, 255, .4) 27%,
    transparent 27%,
    transparent 52%, rgba(255, 255, 255, .4) 52%,
    rgba(255, 255, 255, .4) 77%,
    transparent 77%, transparent
  );
  background-image: -moz-linear-gradient(
    135deg,
    rgba(255, 255, 255, .4) 27%, transparent 27%,
    transparent 52%, rgba(255, 255, 255, .4) 52%,
    rgba(255, 255, 255, .4) 77%, transparent 77%,
    transparent
  );
  background-image: -ms-linear-gradient(
    135deg,
    rgba(255, 255, 255, .4) 27%, transparent 27%,
    transparent 52%, rgba(255, 255, 255, .4) 52%,
    rgba(255, 255, 255, .4) 77%, transparent 77%,
    transparent
  );
  background-image: -o-linear-gradient(
    135deg,
    rgba(255, 255, 255, .4) 27%, transparent 27%,
    transparent 52%, rgba(255, 255, 255, .4) 52%,
    rgba(255, 255, 255, .4) 77%, transparent 77%,
    transparent
  );
  background-image: linear-gradient(
    135deg,
    rgba(255, 255, 255, .4) 27%, transparent 27%,
    transparent 52%, rgba(255, 255, 255, .4) 52%,
    rgba(255, 255, 255, .4) 77%, transparent 77%,
    transparent
  );
}

.markdown-body .progress-100plus .progress-bar {
  background-color: #a6d796;
}

.markdown-body .progress-80plus .progress-bar {
  background-color: #c6d796;
}

.markdown-body .progress-60plus .progress-bar {
  background-color: #d7c896;
}

.markdown-body .progress-40plus .progress-bar {
  background-color: #d7a796;
}

.markdown-body .progress-20plus .progress-bar {
  background-color: #d796a6;
}

.markdown-body .progress-0plus .progress-bar {
  background-color: #c25f77;
}

.markdown-body .candystripe-animate .progress-bar{
  -webkit-animation: animate-stripes 3s linear infinite;
  -moz-animation: animate-stripes 3s linear infinite;
  animation: animate-stripes 3s linear infinite;
}

@-webkit-keyframes animate-stripes {
  0% {
    background-position: 0 0;
  }

  100% {
    background-position: 60px 0;
  }
}

@-moz-keyframes animate-stripes {
  0% {
    background-position: 0 0;
  }

  100% {
    background-position: 60px 0;
  }
}

@keyframes animate-stripes {
  0% {
    background-position: 0 0;
  }

  100% {
    background-position: 60px 0;
  }
}

.markdown-body .gloss .progress-bar {
  box-shadow:
    inset 0 4px 12px rgba(255, 255, 255, .7),
    inset 0 -12px 0 rgba(0, 0, 0, .05);
}

/* Multimarkdown Critic Blocks */
.markdown-body .critic_mark {
  background: #ff0;
}

.markdown-body .critic_delete {
  color: #c82829;
  text-decoration: line-through;
}

.markdown-body .critic_insert {
  color: #718c00 ;
  text-decoration: underline;
}

.markdown-body .critic_comment {
  color: #8e908c;
  font-style: italic;
}

.markdown-body .headeranchor {
  font: normal normal 16px octicons-anchor;
  line-height: 1;
  display: inline-block;
  text-decoration: none;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

.headeranchor:before {
  content: '\f05c';
}

.markdown-body .task-list-item {
  list-style-type: none;
}

.markdown-body .task-list-item+.task-list-item {
  margin-top: 3px;
}

.markdown-body .task-list-item input {
  margin: 0 4px 0.25em -20px;
  vertical-align: middle;
}

/* Media */
@media only screen and (min-width: 480px) {
  .markdown-body {
    font-size:14px;
  }
}

@media only screen and (min-width: 768px) {
  .markdown-body {
    font-size:16px;
  }
}

@media print {
  .markdown-body * {
    background: transparent !important;
    color: black !important;
    filter:none !important;
    -ms-filter: none !important;
  }

  .markdown-body {
    font-size:12pt;
    max-width:100%;
    outline:none;
    border: 0;
  }

  .markdown-body a,
  .markdown-body a:visited {
    text-decoration: underline;
  }

  .markdown-body .headeranchor-link {
    display: none;
  }

  .markdown-body a[href]:after {
    content: " (" attr(href) ")";
  }

  .markdown-body abbr[title]:after {
    content: " (" attr(title) ")";
  }

  .markdown-body .ir a:after,
  .markdown-body a[href^="javascript:"]:after,
  .markdown-body a[href^="#"]:after {
    content: "";
  }

  .markdown-body pre {
    white-space: pre;
    white-space: pre-wrap;
    word-wrap: break-word;
  }

  .markdown-body pre,
  .markdown-body blockquote {
    border: 1px solid #999;
    padding-right: 1em;
    page-break-inside: avoid;
  }

  .markdown-body .progress,
  .markdown-body .progress-bar {
    -moz-box-shadow: none;
    -webkit-box-shadow: none;
    box-shadow: none;
  }

  .markdown-body .progress {
    border: 1px solid #ddd;
  }

  .markdown-body .progress-bar {
    height: 22px;
    border-right: 1px solid #ddd;
  }

  .markdown-body tr,
  .markdown-body img {
    page-break-inside: avoid;
  }

  .markdown-body img {
    max-width: 100% !important;
  }

  .markdown-body p,
  .markdown-body h2,
  .markdown-body h3 {
    orphans: 3;
    widows: 3;
  }

  .markdown-body h2,
  .markdown-body h3 {
    page-break-after: avoid;
  }
}
</style><title>beginner-guide-to-linkers</title></head><body><article class="markdown-body"><h1 id="beginners-guide-to-linkers"><a href="http://www.lurklurk.org/linkers/linkers.html">Beginner&rsquo;s Guide to Linkers</a></h1>
<p>This article is intended to help C &amp; C++ programmers understand the essentials of what the linker does. I&rsquo;ve explained this to a number of colleagues over the years, so I decided it was time to write it down so that it&rsquo;s more widely available (and so that I don&rsquo;t have to explain it again). [Updated March 2009 to include more information on the pecularities of linking on Windows, plus some clarification on the one definition rule.]</p>
<p>A typical example of what triggers this explanation is when I help someone who has a link error like:</p>
<pre><code>g++ -o test1 test1a.o test1b.o
test1a.o(.text+0x18): In function `main':
: undefined reference to `findmax(int, int)'
collect2: ld returned 1 exit status
</code></pre>

<p>If your reaction to this is &lsquo;<a href="#namemangling">almost certainly missing extern &ldquo;C&rdquo;<br />
</a>&lsquo; then you probably already know everything in this article.</p>
<div class="toc">
<ul>
<li><a href="#beginners-guide-to-linkers">Beginner&rsquo;s Guide to Linkers</a><ul>
<li><a href="#naming-of-parts-whats-in-a-c-file">Naming of Parts: What&rsquo;s in a C File</a></li>
<li><a href="#what-the-c-compiler-does">What The C Compiler Does</a><ul>
<li><a href="#dissecting-an-object-file">Dissecting An Object File</a></li>
</ul>
</li>
<li><a href="#what-the-linker-does-part-1">What The Linker Does: Part 1</a><ul>
<li><a href="#duplicate-symbols">Duplicate Symbols</a></li>
</ul>
</li>
<li><a href="#what-the-operating-system-does">What The Operating System Does</a></li>
<li><a href="#what-the-linker-does-part-2">What The Linker Does: Part 2</a><ul>
<li><a href="#static-libraries">Static Libraries</a></li>
<li><a href="#shared-libraries">Shared Libraries</a></li>
<li><a href="#windows-dlls">Windows DLLs</a><ul>
<li><a href="#exporting-symbols">Exporting Symbols</a></li>
<li><a href="#lib-and-other-library-related-files">.LIB and Other Library-Related Files</a></li>
<li><a href="#importing-symbols">Importing Symbols</a></li>
<li><a href="#circular-dependencies">Circular Dependencies</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#adding-c-to-the-picture">Adding C++ To The Picture</a><ul>
<li><a href="#function-overloading-name-mangling">Function Overloading &amp; Name Mangling</a></li>
<li><a href="#initialization-of-statics">Initialization of Statics</a></li>
<li><a href="#templates">Templates</a></li>
</ul>
</li>
<li><a href="#dynamically-loaded-libraries">Dynamically Loaded Libraries</a><ul>
<li><a href="#interaction-with-c-features">Interaction with C++ Features</a></li>
</ul>
</li>
<li><a href="#more-details">More Details</a></li>
</ul>
</li>
</ul>
</div>
<p><span id="cfile"></span></p>
<h2 id="naming-of-parts-whats-in-a-c-file"><a name="user-content-naming-of-parts-whats-in-a-c-file" href="#naming-of-parts-whats-in-a-c-file" class="headeranchor-link" aria-hidden="true"><span class="headeranchor"></span></a>Naming of Parts: What&rsquo;s in a C File</h2>
<p>This section is a quick reminder of the different parts of a C file. If everything in the <a href="#cfilelisting">sample C file listing below</a> makes sense to you, you can probably skip to the <a href="#ccompiler">next section</a>.</p>
<p>The first division to understand is between declarations and definitions. A<em>definition</em> associates a name with an implementation of that name, which could be either data or code:</p>
<p><span id="definition"></span></p>
<ul>
<li>A definition of a variable induces the compiler to reserve some space for that variable, and possibly fill that space with a particular value.</li>
<li>A definition of a function induces the compiler to generate code for that function.</li>
</ul>
<p><span id="declaration"></span></p>
<p>A <em>declaration</em> tells the C compiler that a definition of something (with a particular name) exists elsewhere in the program, probably in a different C file. (Note that a definition also counts as a declaration—it&rsquo;s a declaration that also happens to fill in the particular &ldquo;elsewhere&rdquo;).For variables, the definitions split into two sorts:</p>
<ul>
<li><em>global variables</em>, which exist for the whole lifetime of the program (&ldquo;static extent&rdquo;), and which are usually accessible in lots of different functions</li>
<li><em>local variables</em>, which only exist while a particular function is being executed (&ldquo;local extent&rdquo;) and are only accessible within that function</li>
</ul>
<p>To be clear, by &ldquo;accessible&rdquo; we mean &ldquo;can be referred to using the name associated with the variable by its definition&rdquo;.There are a couple of special cases where things aren&rsquo;t so immediately obvious:</p>
<ul>
<li><em>static</em> local variables are actually global variables, because they exist for the lifetime of the program, even though they&rsquo;re only visible inside a single function<br />
likewise static</li>
<li><em>global</em> variables also count as global variables, even though they can only be accessed by the functions in the particular file where they were defined</li>
</ul>
<p>While we&rsquo;re on the subject of the &ldquo;static&rdquo; keyword, it&rsquo;s also worth pointing out that making a <em>function</em> static just narrows down the number of places that are able to refer to that function by name (specifically, to other functions in the same file).</p>
<p>For both global and local variable definitions, we can also make a distinction between whether the variable is initialized or not—that is, whether the space associated with the particular name is pre-filled with a particular value.</p>
<p>Finally, we can store information in memory that is dynamically allocated using <code>malloc</code> or <code>new</code>. There is no way to refer to the space allocated by name, so we have to use pointers instead—a named variable (the pointer) holds the address of the unnamed piece of memory. This piece of memory can also be <code>deallocated</code> with <code>free</code> or <code>delete</code>, so the space is referred to as having &ldquo;dynamic extent&rdquo;.</p>
<p>Let&rsquo;s put all that together:</p>
<p><span id="ctable"></span></p>
<p><img alt="Paste_Image.png" src="http://upload-images.jianshu.io/upload_images/26219-d2089e84c96b812f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" /></p>
<p>An easier way to follow this is probably just to look at this sample program:</p>
<p><span id="cfilelisting"></span></p>
<pre><code>/* This is the definition of a uninitialized global variable */
int x_global_uninit;

/* This is the definition of a initialized global variable */
int x_global_init = 1;

/* This is the definition of a uninitialized global variable, albeit
 * one that can only be accessed by name in this C file */
static int y_global_uninit;

/* This is the definition of a initialized global variable, albeit
 * one that can only be accessed by name in this C file */
static int y_global_init = 2;

/* This is a declaration of a global variable that exists somewhere
 * else in the program */
extern int z_global;

/* This is a declaration of a function that exists somewhere else in
 * the program (you can add &quot;extern&quot; beforehand if you like, but it's
 * not needed) */
int fn_a(int x, int y);

/* This is a definition of a function, but because it is marked as
 * static, it can only be referred to by name in this C file alone */
static int fn_b(int x)
{
  return x+1;
}

/* This is a definition of a function. */
/* The function parameter counts as a local variable */
int fn_c(int x_local)
{
  /* This is the definition of an uninitialized local variable */
  int y_local_uninit;
  /* This is the definition of an initialized local variable */
  int y_local_init = 3;

  /* Code that refers to local and global variables and other
   * functions by name */
  x_global_uninit = fn_a(x_local, x_global_init);
  y_local_uninit = fn_a(x_local, y_local_init);
  y_local_uninit += fn_b(z_global);
  return (y_global_uninit + y_local_uninit);
}
</code></pre>

<p><span id="ccompiler"></span></p>
<h2 id="what-the-c-compiler-does"><a name="user-content-what-the-c-compiler-does" href="#what-the-c-compiler-does" class="headeranchor-link" aria-hidden="true"><span class="headeranchor"></span></a>What The C Compiler Does</h2>
<p>The C compiler&rsquo;s job is to convert a C file from text that the human can (usually) understand, into stuff that the computer can understand. This output by the compiler as an <em>object file</em>. On UNIX platforms these object files normally have <code>a.o</code> suffix; on Windows they have <code>a .obj</code> suffix. The contents of an object file are essentially two kinds of things:</p>
<ul>
<li><em>code</em>, corresponding to <a href="#definition">definitions</a> of functions in the C file</li>
<li><em>data</em>, corresponding to <a href="#definition">definitions</a> of <strong>global</strong> variables in the C file (for an initialized global variable, the initial value of the variable also has to be stored in the object file).</li>
</ul>
<p>Instances of either of these kinds of things will have names associated with them—the names of the variables or functions whose definitions generated them.</p>
<p>Object code is the sequence of (suitably encoded) machine instructions that correspond to the C instructions that the programmer has written—all of those ifs and whiles and even gotos. All of these instructions need to manipulate information of some sort, and that information needs to be kept somewhere—that&rsquo;s the job of the variables. The code can also refer to other bits of code—specifically, to other C functions in the program.</p>
<p>Wherever the code refers to a variable or function, the compiler only allows this if it has previously seen a <a href="#declaration">declaration</a> for that variable or function—the declaration is a promise that a definition exists somewhere else in the whole program.</p>
<p>The job of the linker is to make good on these promises, but in the meanwhile what does the compiler do with all of these promises when it is generating an object file?</p>
<p>Basically, the compiler leaves a blank. The blank (a &ldquo;reference&rdquo;) has a name associated to it, but the value corresponding to that name is not yet known.</p>
<p>With that in mind, we can depict the object file corresponding to <a href="#cfilelisting">the program given above</a> like this:</p>
<p><span id="objpng"></span></p>
<p><img alt="Paste_Image.png" src="http://upload-images.jianshu.io/upload_images/26219-25113bfdf2c952ca.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" /></p>
<p><span id="objfile"></span></p>
<h3 id="dissecting-an-object-file"><a name="user-content-dissecting-an-object-file" href="#dissecting-an-object-file" class="headeranchor-link" aria-hidden="true"><span class="headeranchor"></span></a>Dissecting An Object File</h3>
<p>We&rsquo;ve kept everything at a high level so far; it&rsquo;s useful to see how this actually works in practice. The key tool for this is the command <strong>nm</strong>, which gives information about the symbols in an object file on UNIX platforms. On Windows, the <a href="http://support.microsoft.com/kb/177429"><strong>dumpbin</strong> command</a> with the <code>/symbols</code> option is roughly equivalent; there is also <a href="http://sourceforge.net/project/showfiles.php?group_id=2435&amp;package_id=11290">a Windows port</a> of the <a href="http://www.gnu.org/software/binutils">GNU binutils</a> tools which includes an<code>nm.exe</code>.</p>
<p>Let&rsquo;s have a look at what <code>nm</code> gives on the object file produced from the <a href="#cfilelisting">C file above</a>:</p>
<pre><code>Symbols from c_parts.o:

Name                  Value   Class        Type         Size     Line  Section

fn_a                |        |   U  |            NOTYPE|        |     |*UND*
z_global            |        |   U  |            NOTYPE|        |     |*UND*
fn_b                |00000000|   t  |              FUNC|00000009|     |.text
x_global_init       |00000000|   D  |            OBJECT|00000004|     |.data
y_global_uninit     |00000000|   b  |            OBJECT|00000004|     |.bss
x_global_uninit     |00000004|   C  |            OBJECT|00000004|     |*COM*
y_global_init       |00000004|   d  |            OBJECT|00000004|     |.data
fn_c                |00000009|   T  |              FUNC|00000055|     |.text
</code></pre>

<p>The output on different platforms can vary a bit (check the <code>man</code> pages to find out more on a particular version), but the key information given is the class of each symbol, and its size (when available). The class can have a number of different values:</p>
<ul>
<li>A class of <strong>U</strong> indicates an undefined reference, one of the &ldquo;blanks&rdquo; mentioned previously. For this object, there are two: &ldquo;<code>fn_a</code>&rdquo; and &ldquo;<code>z_global</code>&rdquo;. (Some versions of <code>nm</code> may also print out a <em>section</em>, which will be <strong><em>UND</em></strong> or <strong>UNDEF</strong> in this case)</li>
<li>A class of <strong>t</strong> or <strong>T</strong> indicates where code is defined; the different classes indicate whether the function is local to this file (<strong>t</strong>) or not (<strong>T</strong>)—i.e. whether the function was originally declared with <code>static</code>. Again, some systems may also show a section, something like <strong>.text</strong></li>
<li>A class of <strong>d</strong> or <strong>D</strong> indicates an initialized global variable, and again the particular class indicates whether the variable is local (<strong>d</strong>) or not (<strong>D</strong>). If there&rsquo;s a section, it will be something like <strong>.data</strong></li>
<li>For an uninitialized global variable, we get <strong>b</strong> if it&rsquo;s static/local, and <strong>B</strong> or <strong>C</strong>when it&rsquo;s not. The section in this case will probably be something like <strong>.bss</strong>or <strong><em>COM</em></strong>.</li>
</ul>
<p>We may also get some symbols that weren&rsquo;t part of the original input C file; we&rsquo;ll ignore these as they&rsquo;re typically part of the compiler&rsquo;s nefarious internal mechanisms for getting your program to link.</p>
<p><span id="linker1"></span></p>
<h2 id="what-the-linker-does-part-1"><a name="user-content-what-the-linker-does-part-1" href="#what-the-linker-does-part-1" class="headeranchor-link" aria-hidden="true"><span class="headeranchor"></span></a>What The Linker Does: Part 1</h2>
<p>We mentioned earlier that a declaration of a function or a variable is a promise to the C compiler that somewhere else in the program is a definition for that function or variable, and that the linker&rsquo;s jobs is to make good on that promise. With <a href="#objpng">the diagram of an object file</a> in front of us, we can also describe this as &ldquo;filling in the blanks&rdquo;.</p>
<p>To illustrate this, let&rsquo;s have a companion C file to the <a href="#cfilelisting">one given previously</a>:</p>
<pre><code>/* Initialized global variable */
int z_global = 11;
/* Second global named y_global_init, but they are both static */
static int y_global_init = 2;
/* Declaration of another global variable */
extern int x_global_init;

int fn_a(int x, int y)
{
  return(x+y);
}

int main(int argc, char *argv[])
{
  const char *message = &quot;Hello, world&quot;;

  return fn_a(11,12);
}
</code></pre>

<p><img alt="Paste_Image.png" src="http://upload-images.jianshu.io/upload_images/26219-b5aa27c3a0f43e1e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" /></p>
<p>With these two diagrams, we can see that all of the dots can joined up (if they couldn&rsquo;t be, then the linker would emit an error message). Every thing has its place, and every place has its thing, and the linker can fill in all of the blanks as shown (on a UNIX system, the linker is typically invoked with <strong><code>ld</code></strong>).</p>
<p><img alt="Paste_Image.png" src="http://upload-images.jianshu.io/upload_images/26219-653ec2aa93a1099e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" /></p>
<p>As for <a href="#objfile">object files</a>, we can use <code>nm</code> to examine the resulting executable file:</p>
<pre><code>Symbols from sample1.exe:

Name                  Value   Class        Type         Size     Line  Section

_Jv_RegisterClasses |        |   w  |            NOTYPE|        |     |*UND*
__gmon_start__      |        |   w  |            NOTYPE|        |     |*UND*
__libc_start_main@@GLIBC_2.0|        |   U  |              FUNC|000001ad|     |*UND*
_init               |08048254|   T  |              FUNC|        |     |.init
_start              |080482c0|   T  |              FUNC|        |     |.text
__do_global_dtors_aux|080482f0|   t  |              FUNC|        |     |.text
frame_dummy         |08048320|   t  |              FUNC|        |     |.text
fn_b                |08048348|   t  |              FUNC|00000009|     |.text
fn_c                |08048351|   T  |              FUNC|00000055|     |.text
fn_a                |080483a8|   T  |              FUNC|0000000b|     |.text
main                |080483b3|   T  |              FUNC|0000002c|     |.text
__libc_csu_fini     |080483e0|   T  |              FUNC|00000005|     |.text
__libc_csu_init     |080483f0|   T  |              FUNC|00000055|     |.text
__do_global_ctors_aux|08048450|   t  |              FUNC|        |     |.text
_fini               |08048478|   T  |              FUNC|        |     |.fini
_fp_hw              |08048494|   R  |            OBJECT|00000004|     |.rodata
_IO_stdin_used      |08048498|   R  |            OBJECT|00000004|     |.rodata
__FRAME_END__       |080484ac|   r  |            OBJECT|        |     |.eh_frame
__CTOR_LIST__       |080494b0|   d  |            OBJECT|        |     |.ctors
__init_array_end    |080494b0|   d  |            NOTYPE|        |     |.ctors
__init_array_start  |080494b0|   d  |            NOTYPE|        |     |.ctors
__CTOR_END__        |080494b4|   d  |            OBJECT|        |     |.ctors
__DTOR_LIST__       |080494b8|   d  |            OBJECT|        |     |.dtors
__DTOR_END__        |080494bc|   d  |            OBJECT|        |     |.dtors
__JCR_END__         |080494c0|   d  |            OBJECT|        |     |.jcr
__JCR_LIST__        |080494c0|   d  |            OBJECT|        |     |.jcr
_DYNAMIC            |080494c4|   d  |            OBJECT|        |     |.dynamic
_GLOBAL_OFFSET_TABLE_|08049598|   d  |            OBJECT|        |     |.got.plt
__data_start        |080495ac|   D  |            NOTYPE|        |     |.data
data_start          |080495ac|   W  |            NOTYPE|        |     |.data
__dso_handle        |080495b0|   D  |            OBJECT|        |     |.data
p.5826              |080495b4|   d  |            OBJECT|        |     |.data
x_global_init       |080495b8|   D  |            OBJECT|00000004|     |.data
y_global_init       |080495bc|   d  |            OBJECT|00000004|     |.data
z_global            |080495c0|   D  |            OBJECT|00000004|     |.data
y_global_init       |080495c4|   d  |            OBJECT|00000004|     |.data
__bss_start         |080495c8|   A  |            NOTYPE|        |     |*ABS*
_edata              |080495c8|   A  |            NOTYPE|        |     |*ABS*
completed.5828      |080495c8|   b  |            OBJECT|00000001|     |.bss
y_global_uninit     |080495cc|   b  |            OBJECT|00000004|     |.bss
x_global_uninit     |080495d0|   B  |            OBJECT|00000004|     |.bss
_end                |080495d4|   A  |            NOTYPE|        |     |*ABS*
</code></pre>

<p>This has all of the symbols from the two objects, and all of the undefined references have vanished. The symbols have also all been reordered so that similar types of things are together, and there are a few added extras to help the operating system deal with the whole thing as an executable program.</p>
<p>There&rsquo;s also a fair number of complicating details cluttering up the output, but if you filter out anything starting with an underscore it gets a lot simpler.</p>
<h3 id="duplicate-symbols"><a name="user-content-duplicate-symbols" href="#duplicate-symbols" class="headeranchor-link" aria-hidden="true"><span class="headeranchor"></span></a>Duplicate Symbols</h3>
<p>The previous section mentioned that if the linker cannot find a definition for a symbol to join to references to that symbol, then it will give an error message. So what happens if there are <em>two</em> definitions for a symbol when it comes to link time?</p>
<p><em>In C++, the situation is straightforward</em>. The language has a constraint known as the <em>one definition rule</em>, which says that there has to be exactly one definition for a symbol when it comes to link time, no more and no less. (The relevant section of the C++ standard is 3.2, which also mentions some exceptions that we&rsquo;ll <a href="#templates">come to later on</a>.)</p>
<p>For C, things are slightly less clear. There has to be exactly one definition of any functions or initialized global variables, but the definition of an uninitialized global variable can be treated as a <em>tentative definition</em>. C then allows (or at least does not forbid) different source files to have tentative definitions for the same object.</p>
<p>However, linkers also have to cope with other programming languages that just C and C++, and the one definition rule isn&rsquo;t always appropriate for them. For example, the normal model for Fortran code is effectively to have a copy of each global variable in every file that references it; the linker is required to fold duplicates by picking one of the copies (the largest version, if they are different sizes) and throw away the rest. (This model is sometimes known as the &ldquo;common model&rdquo; of linking, after the Fortran <code>COMMON</code> keyword.)</p>
<p>As a result, it&rsquo;s actually quite common for UNIX linkers <em>not</em> to complain about duplicate definitions of symbols—at least, not when the duplicate symbol is an uninitialized global variable (this is sometimes known as the &ldquo;relaxed ref/def model&rdquo; of linking). If this worries you (and it probably should), check the documentation for your compiler linker—there may well be a <code>--work-properly</code>option that tightens up the behavior. For example, for the GNU toolchain the <code>-fno-common</code> option to the compiler forces it to put uninitialized variables into the BSS segment rather than generating these <em>common blocks</em>.</p>
<p><span id="os"></span></p>
<h2 id="what-the-operating-system-does"><a name="user-content-what-the-operating-system-does" href="#what-the-operating-system-does" class="headeranchor-link" aria-hidden="true"><span class="headeranchor"></span></a>What The Operating System Does</h2>
<p>Now that the linker has produced an executable program with all of the references to symbols joined up to suitable definitions of those symbols, we need to pause briefly to understand what the operating system does when you run the program.</p>
<p>Running the program obviously involves executing the machine code, so the operating system clearly has to transfer the machine code from the executable file on the hard disk into the computer&rsquo;s memory, where the CPU can get at it. This chunk of the program&rsquo;s memory is known as the <em>code segment</em> or <em>text segment</em>.</p>
<p>Code is nothing without data, so all of the global variables need to have some space in the computer&rsquo;s memory too. However, there&rsquo;s a difference between initialized and uninitialized global variables. Initialized variables have particular values that need to be used to begin with, and these values are stored in the object files and in the executable file. When the program is started, the OS copies these values into the program&rsquo;s memory in the <em>data segment</em>.</p>
<p>For uninitialized variables, the OS can assume that they all just start with the initial value 0, so there&rsquo;s no need to copy any values. This chunk of memory, that gets initialized to 0, is known as the <em>bss segment</em>.</p>
<p>This means that space can be saved in the executable file on disk; the initial values of initialized variables have to be stored in the file, but for the uninitialized variables we just need a count of how much space is needed for them.</p>
<p><img alt="Paste_Image.png" src="http://upload-images.jianshu.io/upload_images/26219-f674e73abc348637.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" /></p>
<p>You may have noticed that all of the discussion of object files and linkers so far has only talked about global variables; there&rsquo;s been no mention of the local variables and dynamically allocated memory <a href="#ctable">mentioned earlier</a>.</p>
<p>These pieces of data don&rsquo;t need any linker involvement, because their lifetime only occurs when the program is running—long after the linker has finished its business. However, for the sake of completeness, we can quickly point out here that:</p>
<ul>
<li>local variables are allocated on a piece of memory known as the <em>stack</em>, which grows and shrinks as different functions are called and complete</li>
<li>dynamically allocated memory is taken from an area known as the <em>heap</em>, and the <code>malloc</code> function keeps track of where all the available space in this area is.</li>
</ul>
<p>We can add in these chunks of memory to complete our picture of what the memory space of a running process looks like. Because both the heap and the stack can change size as the program runs, it&rsquo;s quite common to arrange matters so that the stack grows in one direction while the heap grows in the other. That way, the program will only run out of memory when they meet in the middle (and at that point, the memory space really will be full).</p>
<p><img alt="Paste_Image.png" src="http://upload-images.jianshu.io/upload_images/26219-c4c917812d839dac.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" /></p>
<h2 id="what-the-linker-does-part-2"><a name="user-content-what-the-linker-does-part-2" href="#what-the-linker-does-part-2" class="headeranchor-link" aria-hidden="true"><span class="headeranchor"></span></a>What The Linker Does: Part 2</h2>
<p>Now that we&rsquo;ve covered the <a href="#linker1">very basics</a> of the operation of a linker, we can plough on to describe some more complicating details—roughly in the order that these features were historically added to linkers.</p>
<p>The main observation that affected the function of the linker is this: if lots of different programs need to do the same sorts of things (write output to the screen, read files from the hard disk, etc), then it clearly makes sense to commonize this code in one place and have lots of different programs use it.</p>
<p>This is perfectly feasible to do by just using the same object files when linking different programs, but it makes life much easier if whole collections of related object files are kept together in one easily accessible place: a <em>library</em></p>
<p>(Technical aside: This section completely skips a major feature of the linker:<em>relocation</em>. Different programs will be different sizes, so when the shared library gets mapped into the address space of different programs, it will be at different addresses. This in turn means that all of the functions and variables in the library are in different places. Now, if all of the ways of referring to addresses are relative (&ldquo;the value +1020 bytes from here&rdquo;) rather than absolute (&ldquo;the value at 0x102218BF&rdquo;) this is less of a problem, but this isn&rsquo;t always possible. If not, then all of these absolute addresses need to have a suitable offset added to them—this is relocation. I&rsquo;m not going to mention this topic again, though, because it&rsquo;s almost always invisible to the C/C++ programmer—it&rsquo;s very rare that a linking issue is because of relocation problems)</p>
<p><span id="staticlibs"></span></p>
<h3 id="static-libraries"><a name="user-content-static-libraries" href="#static-libraries" class="headeranchor-link" aria-hidden="true"><span class="headeranchor"></span></a>Static Libraries</h3>
<p>The most basic incarnation of a library is a <em>static library</em>. The previous section mentioned that you could share code by just reusing object files; it turns out that static libraries really aren&rsquo;t much more sophisticated than that.</p>
<p>On UNIX systems, the command to produce a static library is normally <strong>ar</strong>, and the library file that it produces typically has a <code>.a</code> extension. These library files are normally also prefixed with &ldquo;<code>lib</code>&rdquo; and passed to the linker with a &ldquo;-l&rdquo; option followed by the name of the library, without prefix or extension (so &ldquo;<code>-lfred</code>&rdquo; will pick up &ldquo;<code>libfred.a</code>&rdquo;).</p>
<p>(Historically, a program called <code>ranlib</code> also used to be needed for static libraries, in order to build an index of symbols at the start of the library. Nowadays the<code>ar</code> tool tends to do this itself.)</p>
<p>On Windows, static libraries have <code>a .LIB</code> extension and are produced by the <code>LIB</code> tool, but this can be confusing as the same extension is also used for an &ldquo;<em>import library</em>&rdquo;, which just holds a list of the things available in a DLL—see the <a href="#winlibfiles">section on Windows DLLs</a>.</p>
<p>As the linker trundles through its collection of object files to be joined together, it builds a list of the symbols it hasn&rsquo;t been able to resolve yet. When all of the explicitly specified objects are done with, the linker now has another place to look for the symbols that are left on this unresolved list—in the library. If the unresolved symbol is defined in one of the objects in the library, then that object is added in, exactly as if the user had given it on the command line in the first place, and the link continues.</p>
<p>Note the granularity of what gets pulled in from the library: if some particular symbol&rsquo;s definition is needed, the <em>whole object</em> that contains that symbol&rsquo;s definition is included. This means that the process can be one step forwards, one step back—the newly added object may resolve one undefined reference, but it may well come with a whole collection of new undefined references of its own for the linker to resolve.</p>
<p>Another important detail to note is the <em>order</em> of events; the libraries are consulted only when then the normal linking is done, and they are processed <em>in order</em>, left to right. This means that if an object pulled in from a library late in the link line needs a symbol from a library earlier in the link line, the linker won&rsquo;t automatically find it.</p>
<p>An example should help to make this clearer; let&rsquo;s suppose we have the following object files, and a link line that pulls in <code>a.o</code>, <code>b.o</code>, <code>-lx</code>  and <code>-ly</code>.</p>
<p><span id="samplelibs"></span></p>
<p><img alt="Paste_Image.png" src="http://upload-images.jianshu.io/upload_images/26219-f3cf477ec54b9423.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" /></p>
<p>Once the linker has processed <code>a.o</code> and <code>b.o</code>, it will have resolved the references to <code>b2</code> and <code>a3</code>, leaving <code>x12</code> and <code>y22</code> as still undefined. At this point, the linker checks the first library <code>libx.a</code> for these symbols, and finds that it can pull in <code>x1.o</code> to satisfy the <code>x12</code> reference; however, doing so also adds <code>x23</code> and <code>y12</code> to the list of undefined references (so the list is now <code>y22</code>, <code>x23</code> and <code>y12</code>).</p>
<p>The linker is still dealing with <code>libx.a</code>, so the <code>x23</code> reference is easily satisfied, by also pulling in <code>x2.o</code> from <code>libx.a</code>. However, this also adds <code>y11</code> to the list of undefineds (which is now <code>y22</code>, <code>y12</code> and <code>y11</code>). None of these can be resolved further using <code>libx.a</code>, so the linker moves on to <code>liby.a</code>.</p>
<p>Here, the same sort of process applies and the linker will pull in both of <code>y1.o</code> and <code>y2.o</code>. The first of these adds a reference to <code>y21</code>, but since <code>y2.o</code> is being pulled in anyway, that reference is easily resolved. The net of this process is that all of the undefined references have been resolved, and some but not all of the objects in the libraries have been included into the final executable.</p>
<p>Notice that the situation would have been a little different if (say) <code>b.o</code> also had a reference to <code>y32</code>. If this had been the case, the linking of <code>libx.a</code> would have worked the same, but the processing of <code>liby.a</code> would also have pulled in <code>y3.o</code>. Pulling in this object would have added <code>x31</code> to the list of unresolved symbols, and the link would have failed—by this stage the linker has already finished with <code>libx.a</code> and would not find the definition (in <code>x3.o</code>) for this symbol.</p>
<p>(By the way, this example has a cyclic dependency between the two librarieslibx.a<br />
 and <code>liby.a</code>; this is typically a Bad Thing, <a href="#wincircular">particularly on Windows</a>)</p>
<p><span id="sharedlibs"></span></p>
<h3 id="shared-libraries"><a name="user-content-shared-libraries" href="#shared-libraries" class="headeranchor-link" aria-hidden="true"><span class="headeranchor"></span></a>Shared Libraries</h3>
<p>For popular libraries like the C standard library (normally <code>libc</code>), having a static library has an obvious disadvantage—every executable program has a copy of the same code. This can take up a lot of unnecessary disk space, if every single executable file has a copy of <code>printf</code> and <code>fopen</code> and such like.</p>
<p>A slightly less obvious disadvantage is that once a program has been statically linked, the code in it is fixed forever. If someone finds and fixes a bug in <code>printf</code>, then every program has to be linked again in order to pick up the fixed code.</p>
<p>To get around these and other problems, <em>shared libraries</em> were introduced (normally indicated by <code>a .so</code> extension, or <code>.dll</code> on Windows machines and <code>.dylib</code>on Mac OS X). For these kinds of libraries, the normal command line linker doesn&rsquo;t necessarily join up all of the dots. Instead, the regular linker takes a kind of &ldquo;IOU&rdquo; note, and defers the payment of that note until the moment when the program is actually run.</p>
<p>What this boils down to is this: if the linker finds that the definition for a particular symbol is in a shared library, then it doesn&rsquo;t include the definition of that symbol in the final executable. Instead, the linker records the name of symbol and which library it is supposed to come from in the executable file instead.</p>
<p>When the program is run, the operating system arranges that these remaining bits of linking are done &ldquo;just in time&rdquo; for the program to run. Before the <code>main</code> function is run, a smaller version of the linker (often called <code>ld.so</code>) goes through these promissory notes and does the last stage of the link there and then—pulling in the code of the library and joining up all of the dots.</p>
<p>This means that none of the executable files have a copy of the code for <code>printf</code>. If a new, fixed, version of <code>printf</code> is available, it can be slipped in just by <code>changinglibc.so</code>—it&rsquo;ll get picked up the next time any program runs.</p>
<p>There&rsquo;s another big difference with how shared libraries work compared to static libraries, and that shows up in the granularity of the link. If a particular symbol is pulled in from a particular shared library (say <code>printf</code> in <code>libc.so</code>), then the <em>whole</em> of that shared library is mapped into the address space of the program. This is very different from the behavior of a static library, where only the particular objects that held undefined symbols got pulled in.</p>
<p>Put another way, a shared library is itself produced as a result of a run of the linker (rather than just forming a big pile of objects like <code>ar</code> does), with references between objects in the same library getting resolved. Once again,<code>nm</code> is a useful tool for illustrating this: for the <a href="#samplelibs">example libraries above</a> it will produce sets of results for the individual object files when run on a static version of the library, but for the shared version of the library, <code>liby.so</code> has only <code>x31</code> as an undefined symbol. Also, for the library-ordering example at the end of the <a href="#staticlibs">previous section</a>, there wouldn&rsquo;t be a problem: adding a reference to <code>y32</code> into <code>b.c</code> would make no difference, as all of the contents of <code>y3.o</code> and <code>x3.o</code> are already pulled in anyway.</p>
<p>As an aside, another useful tool is <strong><code>ldd</code></strong>; on Unix platforms this shows the set of shared libraries that an executable (or a shared library) depends on, together with an indication of where those libraries are likely to be found. For the program to run successfully, the loader needs to be able to find all of these libraries, together with all of their dependencies in turn. (Typically, the loader looks for libraries in the list of directories held in the <code>LD_LIBRARY_PATH</code> environment variable.)</p>
<pre><code>/usr/bin:ldd xeyes
        linux-gate.so.1 =&gt;  (0xb7efa000)
        libXext.so.6 =&gt; /usr/lib/libXext.so.6 (0xb7edb000)
        libXmu.so.6 =&gt; /usr/lib/libXmu.so.6 (0xb7ec6000)
        libXt.so.6 =&gt; /usr/lib/libXt.so.6 (0xb7e77000)
        libX11.so.6 =&gt; /usr/lib/libX11.so.6 (0xb7d93000)
        libSM.so.6 =&gt; /usr/lib/libSM.so.6 (0xb7d8b000)
        libICE.so.6 =&gt; /usr/lib/libICE.so.6 (0xb7d74000)
        libm.so.6 =&gt; /lib/libm.so.6 (0xb7d4e000)
        libc.so.6 =&gt; /lib/libc.so.6 (0xb7c05000)
        libXau.so.6 =&gt; /usr/lib/libXau.so.6 (0xb7c01000)
        libxcb-xlib.so.0 =&gt; /usr/lib/libxcb-xlib.so.0 (0xb7bff000)
        libxcb.so.1 =&gt; /usr/lib/libxcb.so.1 (0xb7be8000)
        libdl.so.2 =&gt; /lib/libdl.so.2 (0xb7be4000)
        /lib/ld-linux.so.2 (0xb7efb000)
        libXdmcp.so.6 =&gt; /usr/lib/libXdmcp.so.6 (0xb7bdf000)
</code></pre>

<p>The reason for this larger granularity is because modern operating systems are clever enough that you can save more than just the duplicate disk space that happens with static libraries; different running processes that use the same shared library can also share the code segment (but not the data/bss segments—two different processes could be in different places for their <code>strtok</code> after all). In order to do this, the whole library has to be mapped in one go, so that the internal references all line up to the same places—if one process pulled in <code>a.o</code> and <code>c.o</code> and another pulled in <code>b.o</code> and <code>c.o</code>, there wouldn&rsquo;t be any commonality for the OS to leverage.</p>
<h3 id="windows-dlls"><a name="user-content-windows-dlls" href="#windows-dlls" class="headeranchor-link" aria-hidden="true"><span class="headeranchor"></span></a>Windows DLLs</h3>
<p>Although the general principles of shared libraries are roughly similar on Unix platforms and Windows, that are a few details that can catch out the unwary.</p>
<p><span id="winexport"></span></p>
<h4 id="exporting-symbols"><a name="user-content-exporting-symbols" href="#exporting-symbols" class="headeranchor-link" aria-hidden="true"><span class="headeranchor"></span></a>Exporting Symbols</h4>
<p>The most major difference between the two is that symbols are not automatically <em>exported</em> by Windows libraries. On Unix, all of the symbols from all of the object files that were linked into the shared library are visible to users of the library. On Windows, the programmer has to explicitly choose to make particular symbols visible—i.e. to export them.</p>
<p>There are three ways to export a symbol from a Windows DLL (and all three ways can be mixed together in the same library).</p>
<ul>
<li>In the source code, <a href="http://msdn.microsoft.com/en-us/library/3y1sfaz2.aspx">declare the symbol as <code>__declspec(dllexport)</code></a>, thusly:<code>__declspec(dllexport) int my_exported_function(int x, double y);</code></li>
<li>On the invocation of the linker, use the <code>/export:*symbol_to_export*</code> <a href="http://msdn.microsoft.com/en-us/library/y0zzbyt4.aspx">option to <code>LINK.EXE</code></a>.<code>LINK.EXE /dll /export:my_exported_function</code></li>
<li>Get the linker to pull in a <a href="http://msdn.microsoft.com/en-us/library/28d6s79h.aspx"><em>module definition</em> (<code>.DEF</code>) file</a> (by using the/DEF:<em><code>def_file</code></em> linker option), and in that file include an <code>EXPORTS</code> section that contains the symbols you want to export.<br />
<pre><code>EXPORTS
  my_exported_function
  my_other_exported_function
</code></pre></li>
</ul>
<p>Once C++ is added in to the mix, the first of these options is the easiest because the compiler takes care of the <a href="#namemangling">name mangling</a> for you.</p>
<p><span id="winlibfiles"></span></p>
<h4 id="lib-and-other-library-related-files"><code>.LIB</code> and Other Library-Related Files</h4>
<p>This neatly leads on to the second complication with Windows libraries: the information about exported symbols that the linker needs to join things up is not held in the DLL itself. Instead, this information is held in a corresponding <code>.LIB</code> file.</p>
<p>The <code>.LIB</code> file associated with a DLL describes what (exported) symbols are present in the DLL, together with their locations. Any other binary that uses the DLL needs to look in the <code>.LIB</code> file so that it can join up the symbols correctly.</p>
<p>To confuse things, the <code>.LIB</code> extension is also used for static libraries.</p>
<p>In fact, there are a wide variety of different files that can be relevant for Windows libraries. As well as the <code>.LIB</code> file and the (optional) <code>.DEF</code> file mentioned in the previous section, you might see all of the following files associated with your Windows library.</p>
<ul>
<li>
<p>Link <a href="http://msdn.microsoft.com/en-us/library/37b80k4a.aspx">output files</a>:</p>
<ul>
<li><em>library</em><code>.DLL</code>: The library code itself; this is needed (at run-time) by any executable that uses the library.</li>
<li><em>library</em><code>.LIB</code>: An &ldquo;import library&rdquo; file which describes what symbols are where in the output DLL. This file is only produced if the DLL exports some symbols; if no symbols are exported, there is no point in having the.LIB<br />
 file. This file is needed at link-time by anything that uses this library.</li>
<li><em>library</em><code>.EXP</code>: An &ldquo;export file&rdquo; for the library being linked, which is needed when <a href="#wincircular">linking binaries with circular dependencies</a>.</li>
<li><em>library</em><code>.ILK</code>: If the <code>/INCREMENTAL</code> option was specified to the linker so that incremental linking is enabled, this file holds the status of the incremental linking. Needed for any future incremental linking of this library.</li>
<li><em>library</em><code>.PDB</code>: If the <code>/DEBUG</code> option was specified to the linker, this file is a<em>program database</em> containing debugging information for the library.<br />
<code>- *library*</code>.MAP`: If the /MAP<br />
 option was specified to the linker, this file holds a description of the internal layout of the library.</li>
</ul>
</li>
<li>
<p>Link <a href="http://msdn.microsoft.com/en-us/library/hcce369f.aspx">input files</a>:</p>
<ul>
<li><em>library</em><code>.LIB</code>: An &ldquo;import library&rdquo; file which describes what symbols are where in any other DLLs that are needed by the thing being linked.</li>
<li><em>library</em><code>.LIB</code>: A static library file which contains a collection of object files that are needed by the thing being linked. Note the ambiguous use of the.LIB<br />
 extension.</li>
<li><em>library</em><code>.DEF</code>: A &ldquo;module definition&rdquo; file which allows control of various details of the linked library, including the <a href="#winexport">export of symbols</a>.</li>
<li><em>library</em><code>.EXP</code>: An &ldquo;export file&rdquo; for the library being linked, which can indicate that a previous run of <code>LIB.EXE</code> for the library has already created the <code>.LIB</code> file for the library. Relevant when <a href="#wincircular">linking binaries with circular dependencies</a>.</li>
<li><em>library</em><code>.ILK</code>: Incremental linking status file; see above.</li>
<li><em>library</em><code>.RES</code>: Resource file that contains information about the various GUI widgets that the executable uses; these are included in the final binary file.</li>
</ul>
</li>
</ul>
<p>This is contrast to Unix, where most of the information held in these extra files is (usually) just included in the library itself.</p>
<h4 id="importing-symbols"><a name="user-content-importing-symbols" href="#importing-symbols" class="headeranchor-link" aria-hidden="true"><span class="headeranchor"></span></a>Importing Symbols</h4>
<p>As well as requiring DLLs to explicitly declare which <a href="http://www.lurklurk.org/linkers/linkers.html#winexport">symbols they export</a>, Windows also allows binaries that use library code to explicitly declare which symbols they <em>import</em>. This is optional, but gives a speed optimization due to some <a href="http://support.microsoft.com/kb/132044">historical features of 16-bit windows</a>.<br />
To do this, <a href="http://msdn.microsoft.com/en-us/library/8fskxacy.aspx">declare the symbol as <code>__declspec(dllimport)</code></a> in the source code, thusly:</p>
<pre><code>__declspec(dllimport) int function_from_some_dll(int x, double y);
__declspec(dllimport) extern int global_var_from_some_dll;
</code></pre>

<p>It&rsquo;s normal good practice in C to have a single declaration for any function or global variable, held in a header file. This leads to a bit of a conundrum: the code in the DLL that holds the definition of the function/variable needs to<em>export</em> the symbol, but any code outside the DLL needs to <em>import</em> the symbol.</p>
<p>A common way round this is to use a preprocessor macro in the header file.</p>
<pre><code>#ifdef EXPORTING_XYZ_DLL_SYMS
#define XYZ_LINKAGE __declspec(dllexport)
#else
#define XYZ_LINKAGE __declspec(dllimport)
#endif

XYZ_LINKAGE int xyz_exported_function(int x);
XYZ_LINKAGE extern int xyz_exported_variable;
</code></pre>

<p>The C file in the DLL which defines the function and variable ensures that the preprocessor variable <code>EXPORTING_XYZ_DLL_SYMS</code> is <code>#defined</code> before it includes this header file, and so does an export of the symbols. Any other code that pulls in this header file doesn&rsquo;t define the symbol and so indicates an import of the symbols.</p>
<p><span id="wincircular"></span></p>
<h4 id="circular-dependencies"><a name="user-content-circular-dependencies" href="#circular-dependencies" class="headeranchor-link" aria-hidden="true"><span class="headeranchor"></span></a>Circular Dependencies</h4>
<p>One final complication with DLLs is that Windows is stricter than Unix in requiring every symbol to have a resolution at link time. On Unix, it&rsquo;s possible to link a shared library that contains an unresolved symbol that the linker has never seen; in this situation, any other code that pulls in this shared library must provide that symbol, or the program will fail to load. Windows doesn&rsquo;t allow this sort of laxity.</p>
<p>In most systems this isn&rsquo;t a problem. Executables rely on high-level libraries, the high-level libraries rely on lower-level libraries, and everything gets linked in the opposite order—low-level libraries first, then higher-level libraries, and finally the executables that rely on it all.</p>
<p>However, if there are circular dependencies between binaries, then things are trickier. If <code>X.DLL</code> needs a symbol from <code>Y.DLL</code>, and <code>Y.DLL</code> needs a symbol from <code>X.DLL</code>, then there is a chicken-and-egg problem: whichever library is linked first won&rsquo;t be able to find all of its symbols.</p>
<p>Windows does provide a <a href="http://msdn.microsoft.com/en-us/library/kkt2hd12.aspx">way around this</a>, roughly as follows.</p>
<ul>
<li>First, fake a link of library <code>X</code>. Run LIB.EXE<br />
 (not <code>LINK.EXE</code>) to produce an <code>X.LIB</code> file that is the same as would have been produced by <code>LINK.EXE</code>. No <code>X.DLL</code> file is produced, but a <code>X.EXP</code> file does get emitted.</li>
<li>Link library <code>Y</code> as normal; this pulls in the <code>X.LIB</code> file from the previous step, and outputs both a <code>Y.DLL</code> and a <code>Y.LIB</code> file.</li>
<li>Finally link library <code>X</code> properly. This is almost the same as normal, but it additionally includes the <code>X.EXP</code> file created in the first step. As normal, this link will pull in the <code>Y.LIB</code> file from the previous step and will create a <code>X.DLL</code> file. Unlike normal, the link will skip the process of creating an <code>X.LIB</code> file, because there one already there from the first step (which is what the <code>.EXP</code> file indicates).</li>
</ul>
<p>Of course, a better idea is usually to re-organize the libraries so that there aren&rsquo;t any circular dependencies….</p>
<h2 id="adding-c-to-the-picture"><a name="user-content-adding-c-to-the-picture" href="#adding-c-to-the-picture" class="headeranchor-link" aria-hidden="true"><span class="headeranchor"></span></a>Adding C++ To The Picture</h2>
<p>C++ provides a number of extra features over and above what&rsquo;s available in C, and a number of these features interact with the operation of the linker. This wasn&rsquo;t originally the case—the first C++ implementations came as a front end to a C compiler, so the back end of the linker didn&rsquo;t need to be changed—but as time went on, sufficiently sophisticated features were added that the linker had to be enhanced to support them.</p>
<p><span id="namemangling"></span></p>
<h3 id="function-overloading-name-mangling"><a name="user-content-function-overloading-name-mangling" href="#function-overloading-name-mangling" class="headeranchor-link" aria-hidden="true"><span class="headeranchor"></span></a>Function Overloading &amp; Name Mangling</h3>
<p>The first change that C++ allows is the ability to overload a function, so there can be different versions of the same named functions, differing in the types that the function accepts (the function&rsquo;s <em>signature</em>):</p>
<p><span id="maxfns"></span></p>
<pre><code>int max(int x, int y)
{
  if (x&gt;y) return x;
  else return y;
}
float max(float x, float y)
{
  if (x&gt;y) return x;
  else return y;
}
double max(double x, double y)
{
  if (x&gt;y) return x;
  else return y;
}
</code></pre>

<p>This obviously gives a problem for the linker: when some other code refers to <code>max</code>, which one does it mean?The solution that was adopted for this is called <em>name mangling</em>, because all of the information about the function signature is mangled into a textual form, and that becomes the actual name of the symbol as seen by the linker. Different signature functions get mangled to different names, so the uniqueness problem goes away.</p>
<p>I&rsquo;m not going to go into details of the schemes used (which vary from platform to platform anyway), but a quick look at the object file corresponding to the code above gives some hints (remember, <code>nm</code> is your friend!):</p>
<pre><code>Symbols from fn_overload.o:

Name                  Value   Class        Type         Size     Line  Section

__gxx_personality_v0|        |   U  |            NOTYPE|        |     |*UND*
_Z3maxii            |00000000|   T  |              FUNC|00000021|     |.text
_Z3maxff            |00000022|   T  |              FUNC|00000029|     |.text
_Z3maxdd            |0000004c|   T  |              FUNC|00000041|     |.text
</code></pre>

<p>Here we can see that our three functions called <code>max</code> all get different names in the object files, and we can make a fairly shrewd guess that the two letters after the &ldquo;max&rdquo; are encoding the types of the parameters—&ldquo;i&rdquo; for <code>int</code>, &ldquo;f&rdquo; for <code>float</code> and &ldquo;d&rdquo; for <code>double</code> (things get a lot more complex when classes, namespaces, templates and overloaded operators get added into the mangling mix, though!).</p>
<p>It&rsquo;s also worth noting that there will normally be some way of converting between the user-visible names for things (the <em>demangled</em> names) and the linker-visible names for things (the <em>mangled</em> names). This might be a separate program (e.g. c++filt) or a command-line option (e.g. <code>--demangle</code> as an option to GNU nm), which gives results like:</p>
<pre><code>Symbols from fn_overload.o:

Name                  Value   Class        Type         Size     Line  Section

__gxx_personality_v0|        |   U  |            NOTYPE|        |     |*UND*
max(int, int)            |00000000|   T  |              FUNC|00000021|     |.text
max(float, float)            |00000022|   T  |              FUNC|00000029|     |.text
max(double, double)            |0000004c|   T  |              FUNC|00000041|     |.text
</code></pre>

<p>The area where this mangling scheme most commonly trips people up is when C and C++ code is intermingled. All of the symbols produced by the C++ compiler are mangled; all of the symbols produced by the C compiler are just as they appear in the source file. To get around this, the C++ language allows you to put <strong><code>extern "C"</code></strong> around the declaration &amp; definition of a function. This basically tells the C++ compiler that this particular name should not be mangled—either because it&rsquo;s the definition of a C++ function that some C code needs to call, or because it&rsquo;s a C function that some C++ code needs to call.</p>
<p>For the example given right at the start of this page, it&rsquo;s easy to see that there&rsquo;s a good chance someone has forgotten this <code>extern "C"</code> declaration in their link of C and C++ together.</p>
<pre><code>g++ -o test1 test1a.o test1b.o
test1a.o(.text+0x18): In function `main':
: undefined reference to `findmax(int, int)'
collect2: ld returned 1 exit status
</code></pre>

<p>The big hint here is that the error message includes a function signature—it&rsquo;s not just complaining about plain old find <code>max</code> missing. In other words, the C++ code is actually looking for something like &ldquo;<code>_Z7 findmaxii</code>&rdquo; but only finding &ldquo;<code>find max</code>&rdquo;, and so it fails to link.By the way, note that an <code>extern "C"</code> linkage declaration is ignored for member functions (7.5.4 of the C++ standard).</p>
<h3 id="initialization-of-statics"><a name="user-content-initialization-of-statics" href="#initialization-of-statics" class="headeranchor-link" aria-hidden="true"><span class="headeranchor"></span></a>Initialization of Statics</h3>
<p>The next feature that C++ has over C that affects the linker is the ability to have object <em>constructors</em>. A constructor is a piece of code that sets up the contents of an object; as such it is conceptually equivalent to an initializer value for a variable but with the key practical difference that it involves arbitrary pieces of code.</p>
<p>Recall from <a href="#cfile">an earlier section</a> that a global variable can start off with a particular value. In C, constructing the initial value of such a global variable is easy: the particular value is just <a href="#os">copied from the data segment</a> of the executable file into the relevant place in the memory for the soon-to-be-running program.</p>
<p>In C++, the construction process is allowed to be much more complicated than just copying in a fixed value; all of the code in the various constructors for the class hierarchy has to be run, before the program itself starts running properly.</p>
<p>To deal with this, the compiler includes some extra information in the object files for each C++ file; specifically, the list of constructors that need to be called for this particular file. At link time, the linker combines all of these individual lists into one big list, and includes code that goes through the list one by one, calling all of these global object constructors.</p>
<p>Note that the <em>order</em> in which all of these constructors for global objects get called is not defined—it&rsquo;s entirely at the mercy of what the linker chooses to do. (See Scott Meyers&rsquo; Effective C++ for more details—Item 47 in the <a href="http://www.amazon.com/gp/product/0201924889">second edition</a>, Item 4 in the <a href="http://www.amazon.com/gp/product/0321334876">third edition</a>).</p>
<p>We can hunt down these lists by once again using <code>nm</code>. Consider the following C++ file:</p>
<pre><code>class Fred {
private:
  int x;
  int y;
public:
  Fred() : x(1), y(2) {}
  Fred(int z) : x(z), y(3) {}
};

Fred theFred;
Fred theOtherFred(55);
</code></pre>

<p>For this code, the (demangled) output of <code>nm</code> gives:</p>
<pre><code>Symbols from global_obj.o:

Name                  Value   Class        Type         Size     Line  Section

__gxx_personality_v0|        |   U  |            NOTYPE|        |     |*UND*
__static_initialization_and_destruction_0(int, int)|00000000|   t  |              FUNC|00000039|     |.text
Fred::Fred(int)        |00000000|   W  |              FUNC|00000017|     |.text._ZN4FredC1Ei
Fred::Fred()        |00000000|   W  |              FUNC|00000018|     |.text._ZN4FredC1Ev
theFred             |00000000|   B  |            OBJECT|00000008|     |.bss
theOtherFred        |00000008|   B  |            OBJECT|00000008|     |.bss
global constructors keyed to theFred  |0000003a|   t  |              FUNC|0000001a|     |.text
</code></pre>

<p>There are various things here, but the one we&rsquo;re interested in is the two entries with class as <strong>W</strong> (which indicates a &ldquo;weak&rdquo; symbol) and with section names like &ldquo;<code>.gnu.linkonce.t.*stuff*</code>&rdquo;. These are the markers for global object constructors, and we can see that the corresponding &ldquo;Name&rdquo; fields look sensible—one for each of the two constructors used.</p>
<p><span id="templates"></span></p>
<h3 id="templates"><a name="user-content-templates" href="#templates" class="headeranchor-link" aria-hidden="true"><span class="headeranchor"></span></a>Templates</h3>
<p>In an earlier section, we gave <a href="#maxfns">an example</a> of three different versions of a <code>max</code>function, each of which took different types of argument. However, we can see that the lines of source code for these three functions are absolutely identical, and it seems a shame to have to copy and paste identical code.</p>
<p>C++ introduces the idea of <em>templates</em> to allow code like this to be written once and for all. We can create a header file max_template.h<br />
 with the single unique code for <code>max</code>:</p>
<pre><code>template &lt;class T&gt;
T max(T x, T y)
{
  if (x&gt;y) return x;
  else return y;
}
</code></pre>

<p>and include this header file in C++ code to use the templated function:</p>
<pre><code>#include &quot;max_template.h&quot;

int main()
{
  int a=1;
  int b=2;
  int c;
  c = max(a,b);  // Compiler automatically figures out that max&lt;int&gt;(int,int) is needed
  double x = 1.1;
  float y = 2.2;
  double z;
  z = max&lt;double&gt;(x,y); // Compiler can't resolve, so force use of max(double,double)
  return 0;
}
</code></pre>

<p>This C++ file uses both <code>max&lt;int&gt;(int,int)</code> and <code>max&lt;double&gt;(double,double)</code>, but a different C++ file might use different instantiations of the template—say <code>max&lt;float&gt;(float,float)</code> or even <code>max&lt;MyFloatingPointClass&gt;</code> (<code>MyFloatingPointClass</code>, <code>MyFloatingPointClass</code>).</p>
<p>Each of these different instantiations of the template involves different actual machine code, so by the time that the program is finally linked, the compiler and linker need to make sure that every instantiation of the template that is used has code included into the program (and no unused template instantiations are included to bloat the program size).</p>
<p>So how do they do this? There are normally two ways of arranging this: by folding duplicate instantiations, or by deferring instantiation until link time (I like to refer to these as the sane way and the Sun way).</p>
<p>For the duplicate instantiation approach, each object file contains the code for all of the templates that it uses. For the particular example C++ file above, the contents of the object file are:</p>
<pre><code>

Symbols from max_template.o:

Name                  Value   Class        Type         Size     Line  Section

__gxx_personality_v0|        |   U  |            NOTYPE|        |     |*UND*
double max&lt;double&gt;(double, double)   |00000000|   W  |              FUNC|00000041|     |.text._Z3maxIdET_S0_S0_
int max&lt;int&gt;(int, int)   |00000000|   W  |              FUNC|00000021|     |.text._Z3maxIiET_S0_S0_
main                |00000000|   T  |              FUNC|00000073|     |.text
</code></pre>

<p>and we can see that both <code>max&lt;int&gt;(int,int)</code> and <code>max&lt;double&gt;(double,double)</code> are present.These definitions are listed as <em>weak symbols</em>, and this means that when the linker produces the final executable program, it can throw away all but one of these duplicate definitions (and if it&rsquo;s feeling generous, it can check that all the duplicate definitions actually look like they are the same code). The most significant downside of this approach is that all of the individual object files take up much more room on the hard disk.</p>
<p>The other approach (which is used by the Solaris C++ compiler suite) is to include none of the template definitions in the object files, but instead to leave them all as undefined symbols. When it comes to link time, the linker can collect together all of the undefined symbols that actually correspond to template instantiations, and go and generate the machine code for these instantiations there and then.</p>
<p>This saves space in the individual object files, but has the disadvantage that the linker needs to keep track of where the header file containing the source code, and needs to be able to invoke the C++ compiler at link time (which may slow down the link).</p>
<h2 id="dynamically-loaded-libraries"><a name="user-content-dynamically-loaded-libraries" href="#dynamically-loaded-libraries" class="headeranchor-link" aria-hidden="true"><span class="headeranchor"></span></a>Dynamically Loaded Libraries</h2>
<p>The last feature that we&rsquo;ll talk about on this page is the dynamic loading of shared libraries. <a href="#sharedlibs">A previous section</a> described how using shared libraries means that the final link is deferred until the moment when the program is run. On modern systems, it&rsquo;s possible to defer linking to even later than that.</p>
<p>This is done with a pair of system calls,<code>dlopen</code> and <code>dlsym</code> (the rough Windows equivalents of these are called <code>LoadLibrary</code> and <code>GetProcAddress</code>). The first of these takes the name of a shared library and loads it into the address space of the running process. Of course, this extra library may itself have undefined symbols, so this call to <code>dlopen</code> may also trigger the loading of other shared libraries.</p>
<p>The <code>dlopen</code> also allows the choice of whether to resolve all of these references at the instant that the library is loaded (<code>RTLD_NOW</code>), or one by one as each undefined reference is hit (<code>RTLD_LAZY</code>). The first way means that the <code>dlopen</code> call takes much longer, but the second way involves the slight risk that sometime later the program will discover that there is an undefined reference that can&rsquo;t be resolved—at which point, the program will be terminated.</p>
<p>Of course, there&rsquo;s no way for a symbol from the dynamically loaded library to have a name. However, as ever with programming problems, this is easily solved by adding an extra level of indirection—in this case, by using a pointer to the space for the symbol, rather than referring to it by name. The call <code>dlsym</code> takes a string parameter that gives the name of the symbol to be found, and returns a pointer to its location (or <code>NULL</code> if it can&rsquo;t be found).</p>
<h3 id="interaction-with-c-features"><a name="user-content-interaction-with-c-features" href="#interaction-with-c-features" class="headeranchor-link" aria-hidden="true"><span class="headeranchor"></span></a>Interaction with C++ Features</h3>
<p>This dynamic loading feature is all very spangly, but how does it interact with the various C++ features that affect the overall behavior of linkers?</p>
<p>The first observation is that mangled names are a bit tricky. When dlsym<br />
 is called, it takes a string containing the name of the symbol to be found. This has to be the linker-visible version of the name; in other words, the mangled version of the name.</p>
<p>Because the particular name mangling schemes can vary from platform to platform and from compiler to compiler, this means that it&rsquo;s pretty much impossible to dynamically locate a C++ symbol in a portable way. Even if you&rsquo;re happy to stick to one particular compiler and delve around in its internals, there are more problems in store—for anything other than vanilla C-like functions, you have to worry about pulling vtables and such like.</p>
<p>All in all, it&rsquo;s usually best to just stick to having a single, well known <code>extern "C"</code> entrypoint that can be <code>dlsym</code>ed; this entrypoint can be a factory method that returns pointers to full instances of a C++ class, allowing all of the C++ goodness to be accessed.</p>
<p>The compiler can sort out constructors for global objects in a <code>dlopen</code>ed library because there are a couple of special symbols that can be defined in the library and which the linker (whether load-time or run-time) will call when the library is dynamically loaded or unloaded—so the necessary constructor and destructor calls can be put there. In Unix these are functions <code>called _init</code> and <code>_fini</code>, or for more recent systems using the GNU toolchain, these are any functions marked with <code>__attribute__((constructor))</code> or <code>__attribute__((destructor))</code>. In Windows, the relevant function is <code>DllMain</code> with a reason parameter or <code>DLL_PROCESS_ATTACH</code> or <code>DLL_PROCESS_DETACH</code>.</p>
<p>Finally, dynamic loading works fine with the &ldquo;fold duplicates&rdquo; approach to template instantiation, but is much trickier with the &ldquo;compile templates at link time&rdquo; approach—in this case, &ldquo;link time&rdquo; is after the program is running (and possibly on a different machine than holds the source code). Check the compiler &amp; linker documentation for ways round this.</p>
<h2 id="more-details"><a name="user-content-more-details" href="#more-details" class="headeranchor-link" aria-hidden="true"><span class="headeranchor"></span></a>More Details</h2>
<p>The contents of this page have deliberately skipped a lot of details about how linkers work, because I&rsquo;ve found that the level of description here covers 95% of the everyday problems that programmers encounter with the link steps for their programs.</p>
<p>If you want to go further, some additional references are:</p>
<ul>
<li><a href="http://www.amazon.com/exec/obidos/tg/detail/-/1558604960">John Levine, <em>Linkers and Loaders</em></a>: contains lots and lots of information about the details of linkers and loaders work, including all the <a href="http://www.lurklurk.org/linkers/linkers.html#relocation">things I&rsquo;ve skipped</a> here. There also appears to be an online version of it (or an early draft of it) <a href="http://www.iecc.com/linker">here</a></li>
<li><a href="http://0xfe.blogspot.com/2006/03/how-os-x-executes-applications.html">Excellent link</a> on the Mach-O format for binaries on Mac OS X [Added 27-Mar-06]</li>
<li><a href="http://www.amazon.com/exec/obidos/tg/detail/-/0131774298/">Peter Van Der Linden, <em>Expert C Programming</em></a>: excellent book which includes more information about how C code <a href="http://www.lurklurk.org/linkers/linkers.html#os">transforms into a running program</a> than any other C text I&rsquo;ve encountered</li>
<li><a href="http://www.amazon.com/exec/obidos/tg/detail/-/020163371X">Scott Meyers, <em>More Effective C++</em></a>: Item 34 covers the pitfalls of combining C and C++ in the same program (whether linker-related or not).</li>
<li><a href="http://www.amazon.com/exec/obidos/tg/detail/-/0201543303/">Bjarne Stroustrup, <em>The Design and Evolution of C++</em></a>: section 11.3 discusses<a href="http://www.lurklurk.org/linkers/linkers.html#cppcompiler">linkage in C++</a> and how it came about</li>
<li><a href="http://www.amazon.com/exec/obidos/tg/detail/-/0201514591/">Margaret A. Ellis &amp; Bjarne Stroustrup, <em>The Annotated C++ Reference Manual</em></a>: section 7.2c describes one <a href="http://www.lurklurk.org/linkers/linkers.html#namemangling">particular name mangling scheme</a></li>
<li><a href="http://www.skyfree.org/linux/references/ELF_Format.pdf">ELF format reference [PDF]</a></li>
<li>Two interesting articles on <a href="http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html">creating tiny Linux executables</a> and a <a href="http://blog.ksplice.com/2010/03/libc-free-world/">minimal Hello World</a> in particular.</li>
<li><a href="http://www.akkadia.org/drepper/dsohowto.pdf">&ldquo;How To Write Shared Libraries&rdquo; [PDF]</a> by <a href="http://www.akkadia.org/drepper/">Ulrich Drepper</a> has more details on ELF and relocation.</li>
</ul>
<p>Many thanks to Mike Capp and Ed Wilson for useful suggestions about this page.</p>
<hr />
<p>Copyright (c) 2004-2005,2009-2010 David Drysdale</p>
<p>Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is available <a href="http://www.gnu.org/copyleft/fdl.html">here</a>.</p>
<p><a href="mailto:dmd at_sign_here lurklurk dot_here org">Contact me</a></p></article></body></html>